package com.test.cordova.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.test.cordova.R;

/**
 * https://blog.csdn.net/ming_147/article/details/53997769
 *
 */
public class ChartView extends View {
    private static final String TAG = ChartView.class.getSimpleName();
    private Paint mPaint;

    public ChartView(Context context) {
        super(context);
        initPaint();
    }

    public ChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChartView);

        a.recycle();
        initPaint();
    }

    private void initPaint() {
        setBackgroundColor(Color.parseColor("#222222"));
        mPaint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        Log.e(TAG, "w: " + w + "h: " + h + "oldw: " + oldw + "oldh: " + oldh);
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvasTitle(canvas);
        canvasXY(canvas);
        canvasLine(canvas);
    }

    private void canvasLine(Canvas canvas) {
        int marginBottom = this.getBottom() - 100;
        int marginLeft = 100;

        float brokenSizeY = (marginBottom - marginLeft) / 500;//得到纵轴刻度值
        float brokenSizeX = (this.getRight() - 20 - marginLeft) / 144;

        float lastX = brokenTimes[0]*brokenSizeX + marginLeft,lastY = marginBottom-broken[0]*brokenSizeY;

        for (int i = 0; i < broken.length; i++) {
            float height = marginBottom - broken[i]*brokenSizeY;
            float width = brokenTimes[i]*brokenSizeX+marginLeft;
            canvas.drawCircle(width,height,10,mPaint);
            canvas.drawLine(lastX,lastY,width,height,mPaint);

            lastX = width;
            lastY = height;
        }

    }

    private float[] broken= {0f,30f,75f,160f,66f,170f,80f,120f,130f,150f,138f,180f,200f, 220f,300f,235f,245f,260f,210f,270f,290f,270f,270f,250f,210f,180f,260f,280f,320f};
    private int[] brokenTimes= {0,5,10,15,20,25,30,35,40,45,50,55,60, 65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140};



    private String[] times = {"5:00", "10:00", "15:00", "20:00", "24:00"};
    private String[] atmosphere = {"优", "好", "良", "中", "差"};
    private int[] atmosphereNum = {100, 200, 300, 400, 500};

    private void canvasXY(Canvas canvas) {
         int marginBottom = this.getBottom() - 100;
         int marginLeft = 100;
        //初始化X轴
        mPaint.setColor(Color.WHITE);
        canvas.drawLine(marginLeft, marginLeft, marginLeft, marginBottom, mPaint);
        //初始化Y轴
        mPaint.setStrokeWidth(6);
        canvas.drawLine(marginLeft, marginBottom, this.getRight() - 20, marginBottom, mPaint);

        int xWidth = (this.getRight() - 20 - marginLeft) / 5;
        int xLine = marginLeft;//起始位置
        Integer[] xPoint = new Integer[5];
        for (int a = 0; a < 5; a++) {
            xLine += xWidth;
            xPoint[a] = xLine;
        }
        //绘制x轴
        for (int i = 0; i < xPoint.length; i++) {
            mPaint.setColor(Color.parseColor("#FF00FF"));
            mPaint.setTextSize(40);
            mPaint.setTypeface(Typeface.DEFAULT);
            mPaint.setTextAlign(Paint.Align.RIGHT);
            if (i == 0) {
                canvas.drawText("0", marginLeft, marginBottom + 50, mPaint);
                canvas.drawText(times[i], xPoint[i], marginBottom + 50, mPaint);
            } else {
                canvas.drawText(times[i], xPoint[i], marginBottom + 50, mPaint);
            }
        }
        //绘制Y轴
        int yHeight = (marginBottom - marginLeft) / 5;
        for (int i = 0; i < xPoint.length; i++) {
            mPaint.setStrokeWidth(6);
            int startY = marginBottom - yHeight * (i+1);
            int endY = marginBottom - yHeight * i;

            switch (i) {
                case 0:
                    mPaint.setColor(Color.GREEN);
                    break;
                case 1:
                    mPaint.setColor(Color.YELLOW);
                    break;
                case 2:
                    mPaint.setColor(0xffff7e00);
                    break;
                case 3:
                    mPaint.setColor(Color.RED);
                    break;
                case 4:
                    mPaint.setColor(0xff8e1752);
                    break;
            }
            //绘制颜色线
            canvas.drawLine(marginLeft,startY,marginLeft,endY,mPaint);
            //绘制空气污染值
            canvas.drawText(atmosphereNum[i]+"",marginLeft-20,startY,mPaint);
            //绘制污染等级
            canvas.drawText(atmosphere[i],marginLeft-20,endY-yHeight/2,mPaint);
        }

    }

    private void canvasTitle(Canvas canvas) {
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(23);
        mPaint.setTypeface(Typeface.create("宋体", Typeface.BOLD));
        canvas.drawText("帝都空气质量趋势图(author:AbnerMing)", 100, 50, mPaint);
    }

}
